/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.ucase.criminaltransfer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.utils.BaseUtil;
import com.icinfo.cloud.provider.common.utils.CustomCheckParamUtil;
import com.icinfo.cloud.provider.peripheralinterface.lawenforce.dto.EvidenceInfoSaveDto;
import com.icinfo.cloud.provider.peripheralinterface.lawenforce.dto.FileInfoSaveDto;
import com.icinfo.cloud.provider.punish.ucase.criminaltransfer.mapper.CaseCriminalTransferFileMapper;
import com.icinfo.cloud.provider.punish.ucase.criminaltransfer.model.CaseCriminalTransferFile;
import com.icinfo.cloud.provider.punish.ucase.criminaltransfer.service.ICaseCriminalTransferFileService;
import com.icinfo.cloud.provider.punish.ucase.punishinform.dto.CaseCriminalTransferFileSaveDto;
import com.icinfo.cloud.provider.punish.ucase.punishinform.vo.CaseCriminalTransferFileVo;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述: 涉刑案件移送文件表 case_criminal_transfer_file 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年10月10日
 */
@Service
public class CaseCriminalTransferFileServiceImpl extends MyBatisServiceSupport implements ICaseCriminalTransferFileService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(CaseCriminalTransferFileServiceImpl.class);

    @Resource
    private CaseCriminalTransferFileMapper caseCriminalTransferFileMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveCaseCriminalTransferFile(String caseId, List<CaseCriminalTransferFileSaveDto> caseCriminalTransferMaterialVos) {
        UserDTO userInfo = UserUtil.getUserInfo();
        // 删除原来得移送文件
        Example example = new Example(CaseCriminalTransferFile.class);
        example.createCriteria().andEqualTo("caseId", caseId);
        example.orderBy("sortNo asc");
        caseCriminalTransferFileMapper.deleteByExample(example);
        CaseCriminalTransferFile caseCriminalTransferFile = new CaseCriminalTransferFile();
        LocalDateTime now = LocalDateTime.now();
        for (int i = 0; i < caseCriminalTransferMaterialVos.size(); i++) {
            CustomCheckParamUtil.doCheck(caseCriminalTransferMaterialVos.get(i));
            BeanUtils.copyProperties(caseCriminalTransferMaterialVos.get(i), caseCriminalTransferFile);
            caseCriminalTransferFile.setId(BaseUtil.createUid());
            caseCriminalTransferFile.setCaseId(caseId);
            //文件类型暂时全是文件
            if (ObjectUtil.isEmpty(caseCriminalTransferMaterialVos.get(i).getFileType())) {
                caseCriminalTransferFile.setFileType("pdf");
            }
            caseCriminalTransferFile.setUploadUserId(String.valueOf(userInfo.getUserId()));
            caseCriminalTransferFile.setUploadUserName(userInfo.getRealName());
            caseCriminalTransferFile.setUploadTime(now);
            caseCriminalTransferFile.setSortNo(i+1);
            caseCriminalTransferFileMapper.insertSelective(caseCriminalTransferFile);
        }
        return 1;
    }

    @Override
    public List<CaseCriminalTransferFileVo> getCaseCriminalTransferFileListByCaseId(String caseId) {
        Example example = new Example(CaseCriminalTransferFile.class);
        example.createCriteria().andEqualTo("caseId", caseId);
        example.orderBy("sortNo").asc();
        List<CaseCriminalTransferFile> caseCriminalTransferFiles = caseCriminalTransferFileMapper.selectByExample(example);
        List<CaseCriminalTransferFileVo> voList = new ArrayList<>();
        for (CaseCriminalTransferFile file : caseCriminalTransferFiles) {
            CaseCriminalTransferFileVo fileVo = new CaseCriminalTransferFileVo();
            BeanUtils.copyProperties(file, fileVo);
            voList.add(fileVo);
        }
        // 将移送书和终结报告加进去
        return voList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveCaseCriminalTransferFileNew(String caseId, List<EvidenceInfoSaveDto> evidenceInfoDtoList) {
        UserDTO userInfo = UserUtil.getUserInfo();
        // 删除原来得移送文件
        Example example = new Example(CaseCriminalTransferFile.class);
        example.createCriteria().andEqualTo("caseId", caseId);
        example.orderBy("sortNo asc");
        caseCriminalTransferFileMapper.deleteByExample(example);

        LocalDateTime now = LocalDateTime.now();
        for (int i = 0; i < evidenceInfoDtoList.size(); i++) {
            CaseCriminalTransferFile caseCriminalTransferFile = new CaseCriminalTransferFile();

            EvidenceInfoSaveDto evidenceInfoSaveDto = evidenceInfoDtoList.get(i);
            caseCriminalTransferFile.setId(BaseUtil.createUid());
            caseCriminalTransferFile.setCaseId(caseId);
            caseCriminalTransferFile.setEvidenceType(evidenceInfoSaveDto.getEvidenceType());
            caseCriminalTransferFile.setEvidenceTypeName(evidenceInfoSaveDto.getEvidenceTypeCN());

            FileInfoSaveDto fileInfoSaveDto = evidenceInfoSaveDto.getFileInfoDtoList().get(0);
            caseCriminalTransferFile.setFileType(ObjectUtil.isEmpty(fileInfoSaveDto.getFileType()) ? "pdf" : fileInfoSaveDto.getFileType());
            //文件类型暂时全是文件
            caseCriminalTransferFile.setFileName(fileInfoSaveDto.getFileName());
            caseCriminalTransferFile.setFileUrl(fileInfoSaveDto.getFileUrl());
            caseCriminalTransferFile.setFileOwnId(fileInfoSaveDto.getFileOwnId());
            caseCriminalTransferFile.setTransferFileSource(ObjectUtils.isEmpty(fileInfoSaveDto.getTransferFileSource()) ? "1" : fileInfoSaveDto.getTransferFileSource());
            caseCriminalTransferFile.setUploadUserId(String.valueOf(userInfo.getUserId()));
            caseCriminalTransferFile.setUploadUserName(userInfo.getRealName());
            caseCriminalTransferFile.setUploadTime(now);
            caseCriminalTransferFile.setSortNo(i+1);
            caseCriminalTransferFileMapper.insertSelective(caseCriminalTransferFile);
        }
        return 1;
    }
}
